import { defineStore } from 'pinia'
import { getLogins,getCorp,getEmitData,getfinanceData,addEmitData,getBoTun,getOrOrder,getUser } from '@/api/custom'
import { Login_Type,Login_Res } from '@/type/login'

export const useCounterStore = defineStore('finance', {
    state: () => ({
        //客户用来进行添加的
        emitData:<any>{}
    }),
    getters: {
    },
    actions: {
        async interface_getLogin(obj:Login_Type){
            console.log(obj,'我是传递过来的')

          const data:any =  await getLogins(obj)
          console.log(data,'woshi')

          let res:Login_Res = {
            code:200,
            msg:'正常'
          }
          if(data.length !== 0){
             if(obj.password === data[0].pass){
                res = {
                    code:200,
                    msg:'登录成功',
                    data:data[0]
                }
             }else{
                res = {
                    code:200,
                    msg:'密码错误',
                }
             }
            }else{
                res = {
                    code:200,
                    msg:'没有该用户'
                }
            }
           return res
        },
        async interface_getCorp(){
            const data = await getCorp()
            return data
        },
        async interface_getfinance(){
          const data:any =  await getfinanceData()
          return data
        },
        async interface_getBotun(){
            const data:any = await getBoTun()
            return data
        },
        async interface_getOrOrder(){
            const data:any = await getOrOrder()
            return data
        },
        async interface_getUser(){
            const data:any = await getUser()
            return data
        },
        getEmitData(val: string){
            this.emitData = JSON.parse(val)
            // console.log(this.emitData)
        },
        getUpdateData(val:any){
            // console.log(val,'我是pinia传递过来的值')
            console.log(this.emitData)
            if(this.emitData.id){
                // console.log('存在')
                let obj = {
                    corpName: val.corpName,
                    corpState: val.corpState,
                    starRating: val.aaa,
                    address: val.native,
                    industry: val.suoshu,
                    primary_contact: val.industry,
                    title: val.title,
                    phone_number: val.phone_number,
                    enterprise:val.enterprise,
                    Aka:val.Aka,
                    Task:val.Task,
                    WechatPhone:val.WechatPhone,
                    ascription:val.ascription,
                    author:val.author,
                    type:val.type,
                    user_type:val.user_type,
                    region:val.region
                }
                this.emitData = Object.assign(this.emitData,obj)
                getEmitData(this.emitData.id,this.emitData)
            }else{
                    console.log('我的')
                    let obj = {
                        corpName: val.corpName,
                        corpState: val.corpState,
                        starRating: val.aaa,
                        address: val.native,
                        industry: val.suoshu,
                        primary_contact: val.industry,
                        title: val.title,
                        phone_number: val.phone_number,
                        enterprise:val.enterprise,
                        Aka:val.Aka,
                        last_follow_up_date: "1982-08-09",
                        Task:val.Task,
                        WechatPhone:val.WechatPhone,
                        ascription:val.ascription,
                        author:val.author,
                        type:val.type,
                        user_type:val.user_type,
                        region:val.region
                    }
                    this.emitData = Object.assign(this.emitData,obj)
                    console.log(this.emitData)
                    addEmitData(this.emitData)
            }
        },
        getfollowUp(val:any){
            console.log(val)
        }
    }
})